package com.jwthhealth_pub.old.subscribe.address;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;
import com.jwthhealth_pub.old.subscribe.address.adapter.SubscribeHistoryAddressAdatper;
import com.jwthhealth_pub.old.subscribe.model.SubscrbeAddList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 预约历史位置
 * Created by Administrator on 2017/7/18.
 */

public class SubscribeHistoryAddressActivity extends BaseActivity {

    private static final String TAG = "SubscribeHistoryAddressActivity";

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.address_list)
    public RecyclerView addressList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe_hestory_ddrress);
        ButterKnife.bind(this);

        LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        if (userinfoModel != null) {
            Call<SubscrbeAddList> call = ApiHelper.getSubscribeList(userinfoModel.getId(), userinfoModel.getAndroidtoken());
            call.enqueue(new Callback<SubscrbeAddList>() {
                @Override
                public void onResponse(Call<SubscrbeAddList> call, Response<SubscrbeAddList> response) {
                    final SubscrbeAddList body = response.body();
                    if (body == null || body.getCode() == null) return;
                    if (body.getCode().equals("0")) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                addressList.setLayoutManager(new LinearLayoutManager(SubscribeHistoryAddressActivity.this));
                                addressList.setAdapter(new SubscribeHistoryAddressAdatper(SubscribeHistoryAddressActivity.this, body.getData()));
                            }
                        });
                    } else {
                        toas(body.getMsg());
                    }
                }

                @Override
                public void onFailure(Call<SubscrbeAddList> call, Throwable t) {
                    LogUtil.e(t.toString(), TAG);
                }
            });
        }
    }

    @Override
    protected void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        startActivity(new Intent(this, SubscribeProvinceActivity.class));
    }
}
